package com.yf.course.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yf.ability.log.annon.LogInject;
import com.yf.ability.log.enums.LogType;
import com.yf.base.api.annon.DataProtect;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.BaseStateReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.course.modules.course.dto.CourseDTO;
import com.yf.course.modules.course.dto.CourseRefPointDTO;
import com.yf.course.modules.course.dto.KnowladgeDTO;
import com.yf.course.modules.course.dto.KnowledgePointDTO;
import com.yf.course.modules.course.dto.ext.CourseDetailExtDTO;
import com.yf.course.modules.course.dto.ext.CourseDetailExtQuDTO;
import com.yf.course.modules.course.dto.ext.CourseDetailExtRepoDTO;
import com.yf.course.modules.course.dto.request.CourseNotifyReqDTO;
import com.yf.course.modules.course.dto.request.UserCourseReqDTO;
import com.yf.course.modules.course.dto.response.UserCourseRespDTO;
import com.yf.course.modules.course.entity.Course;
import com.yf.course.modules.course.entity.CourseRefPoint;
import com.yf.course.modules.course.entity.CourseRepo;
import com.yf.course.modules.course.service.CourseFileService;
import com.yf.course.modules.course.service.CourseRefPointService;
import com.yf.course.modules.course.service.CourseRepoService;
import com.yf.course.modules.course.service.CourseService;
import com.yf.repo.modules.repo.dto.ext.RepoDetailDTO;
import com.yf.repo.modules.repo.service.RepoService;
import com.yf.system.modules.user.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程信息控制器
 * </p>
 *
 * @author 聪明笨狗
 * @since 2020-12-17 11:28
 */
@Api(tags = {"课程信息"})
@RestController
@RequestMapping("/api/course/course")
public class CourseController extends BaseController {

    @Autowired
    private CourseService baseService;
    @Autowired
    private CourseFileService courseFileService;
    @Autowired
    private RepoService repoService;
    @Autowired
    private CourseRepoService courseRepoService;

    /**
     * 添加或修改
     *
     * @param reqDTO
     * @return
     */
    @LogInject(title = "添加或修改课程", logType = LogType.COURSE)
    @RequiresPermissions(value = {"course:add", "course:update"}, logical = Logical.OR)
    @DataProtect(clazz = Course.class, update = true)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest save(@RequestBody CourseDetailExtDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    /**
     * 批量删除
     *
     * @param reqDTO
     * @return
     */
    @LogInject(title = "批量删除课程", logType = LogType.COURSE)
    @RequiresPermissions("course:delete")
    @DataProtect(clazz = Course.class, delete = true)
    @ApiOperation(value = "批量删除")
    @PostMapping("/delete")
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
     * 查找详情
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<CourseDetailExtDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        CourseDetailExtDTO dto = baseService.detail(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 简略的详情，用于一些简单的地方
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @PostMapping("/simple-detail")
    public ApiRest<CourseDTO> simpleDetail(@RequestBody BaseIdReqDTO reqDTO) {
        Course course = baseService.getById(reqDTO.getId());
        CourseDTO respDTO = new CourseDTO();
        BeanMapper.copy(course, respDTO);
        return super.success(respDTO);
    }

    /**
     * 分页查找
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<CourseDTO>> paging(@RequestBody PagingReqDTO<CourseDTO> reqDTO) {
        //分页查询并转换
        IPage<CourseDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }


    /**
     * 修改状态
     *
     * @param reqDTO
     * @return
     */
    @RequiresPermissions(value = {"course:update"}, logical = Logical.OR)
    @LogInject(title = "启用或禁用课程", logType = LogType.COURSE)
    @ApiOperation(value = "修改状态")
    @PostMapping("/state")
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {

        // 条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(Course::getId, reqDTO.getIds());

        Course record = new Course();
        record.setState(reqDTO.getState());
        baseService.update(record, wrapper);
        return super.success();
    }


    /**
     * 查找用户课程学习情况
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/user-paging")
    public ApiRest<IPage<UserCourseRespDTO>> userPaging(@RequestBody PagingReqDTO<UserCourseReqDTO> reqDTO) {
        //分页查询并转换
        IPage<UserCourseRespDTO> page = baseService.userPaging(reqDTO);
        return super.success(page);
    }


    /**
     * 发送学习安排通知
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "发送学习安排通知")
    @PostMapping("/prepare-notify")
    public ApiRest notify(@RequestBody CourseNotifyReqDTO reqDTO) {
        baseService.prepareNotify(reqDTO);
        return super.success();
    }

    @ApiOperation(value = "更新课件上传完成状态", notes = "更新课件上传完成状态")
    @PostMapping("/addKnowledgePoint")
    public ApiRest addKnowledgePoint(@RequestBody KnowladgeDTO knowledgeDTO) {
        courseFileService.addKnowledgePoint(knowledgeDTO);
        return super.success();
    }

    @ApiOperation(value = "添加课程知识点")
    @PostMapping("/knowledge-point")
    public ApiRest addCourseKnowledgePoint(@RequestBody KnowledgePointDTO knowledgePointDTO) throws IOException {
        baseService.addCourseKnowledgePoint(knowledgePointDTO);
        return super.success();
    }

    @ApiOperation(value = "通过课程ID查找题库详情")
    @PostMapping("/detail-courseId")
    public ApiRest<Object> detailByCourseId(@RequestBody BaseIdReqDTO reqDTO) {
        CourseRepo courseRepo = courseRepoService.getById(reqDTO.getId());
//        CourseRepo courseRepo = courseRepoService.getById("1905238932543610881");
        RepoDetailDTO dto;
        if (courseRepo == null) {
            return super.success("该课程暂无关联题库");
        } else {
            dto = repoService.detail(courseRepo.getRepoId());
        }
        return super.success(dto);
    }

    @ApiOperation(value = "查询用户所创建课程的信息，包含对应题库的题目数量")
    @PostMapping("/detail-repo")
    public ApiRest<List<CourseDetailExtRepoDTO>> detailRepo() {
        String userId = UserUtils.getUserId();
        List<CourseDetailExtRepoDTO> courseRepoDTOS = baseService.detailRepo(userId);
//        List<CourseDetailExtRepoDTO> courseRepoDTOS = baseService.detailRepo("1891743847004782593");
        return super.success(courseRepoDTOS);
    }

    @ApiOperation(value = "查询对应课程ID的课程信息，包含对应题库中的所有题目")
    @PostMapping("/detail-qu")
    public ApiRest<CourseDetailExtQuDTO> detailQu(@RequestBody BaseIdReqDTO reqDTO) {
        CourseDetailExtQuDTO courseRepoDTOS = baseService.detailQu(reqDTO.getId());
//        CourseDetailExtQuDTO courseRepoDTOS = baseService.detailQu("1905238932543610881");
        return super.success(courseRepoDTOS);
    }

    @ApiOperation(value = "查询课程知识点")
    @PostMapping("/get-knowledge-point")
    public ApiRest<KnowledgePointDTO> getCourseKnowledgePoint(@RequestBody KnowledgePointDTO knowledgePointDTO) throws IOException {
        KnowledgePointDTO respDTO = baseService.getCourseKnowledgePoint(knowledgePointDTO);
        if (respDTO == null) {
            return super.failure("没有查询到课程知识点");
        } else {
            return super.success(respDTO);
        }
    }
}
